package com.ssm.helong.shop.controller;

import com.ssm.helong.shop.entity.ShoppingCar;
import com.ssm.helong.shop.service.ShoppingCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.DateFormat;
import java.util.*;

@Controller
@RequestMapping("/shoppingCar")
public class ShoppingCarController {
    @Autowired
    private ShoppingCarService shoppingCarService;

    @RequestMapping("/addshops")
    public String addshops(ShoppingCar shoppingCar) {

        //获取本地时间
        Date date = new Date();
        DateFormat df = DateFormat.getDateTimeInstance();
        shoppingCar.setGoodsId(shoppingCar.getGoodsId());
        shoppingCar.setGoodsNum(shoppingCar.getGoodsNum());
        shoppingCar.setGoodsCreateDate(df.format(date));
        shoppingCar.setUserId(shoppingCar.getUserId());

        int rows = shoppingCarService.addshops(shoppingCar);

        System.out.println("添加商品成功");
        return "addshops";
    }

    //删除购物车中商品
    @RequestMapping("/deleteshop")
    public String getdelete(Integer id) {
        shoppingCarService.getdelete(id);
        System.out.println("移除本商品");
        return "deleteshop";
    }

    //修改购物车商品
    @RequestMapping("/updateNum")
    public String updatenum(ShoppingCar shoppingCar) {
        shoppingCarService.updateNum(shoppingCar);

        System.out.println("修改本商品");
        return "updateNum";
    }

    //查询某用户购物车信息
    @RequestMapping("findUserCar")
    public String findusercar(Integer userId) {
        List a = shoppingCarService.findusercar(userId);
        System.out.println("商品信息" + a);
        return "findUserCar";
    }

    //计算购物车总价值
    @RequestMapping("/totalValue")
    public String totalvalue(Integer userId) {
        int sum = shoppingCarService.totalvalue(userId);

        System.out.println("总价格=" + sum);
        return "totalValue";
    }


    //查询某个用户购物车中选中的商品总价格（选取某些）
    @RequestMapping("selectShops")
    public String selectShops(Integer userId, Integer goodsId[]) {
        int sum1 = shoppingCarService.selectShops(userId, goodsId);
        System.out.println("总价格=" + sum1);
        return "selectShops";
    }
}

